package com.fjwt.gz.components.oss.model;

import com.fjwt.gz.components.oss.constant.OssSavePlaceEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/*
* 定义文件上传的配置信息
*
* @author wg
* @site https://pt.jdyd.xyz/
* @date 2021/6/8 16:38
*/
@Data
@AllArgsConstructor
public class OssFileConfig {

    /** 上传oss文件类型 **/
    public interface BIZ_TYPE {
        String H5 = "H5"; /** H5资源 **/
        String WECHAT = "WeChat"; /** 小程序公众号资源 **/
        String ACTIVITY = "activity"; /** 活动资源 **/
        String DOWNLOAD = "download"; /** 下载资源 **/
        String UPLOAD = "upload"; /** 上传资源 **/
        String TEMPLATE = "template"; /** 模版资源 **/
        String USER = "user"; /** 用户资源 **/
    }

    /** 图片、excel、视频 类型后缀格式 **/
    public static final Set<String> IMG_SUFFIX = new HashSet<>(Arrays.asList("doc","docx","jpg", "png", "jpeg", "gif", "ico", "svg", "bmp", "xlsx", "xls", ".csv" ,"mp4", "ogg", "flv", "avi", "wmv", "rmvb","zip"));

    /** 证书相关文件格式 **/
    public static final Set<String> CERT_SUFFIX = new HashSet<>(Arrays.asList("crt", "p12", "pem", "pfx", "cer", "sm2"));

    /** 黑白名单excel **/
    public static final Set<String> EXCEL_SUFFIX = new HashSet<>(Arrays.asList("xlsx", "xls"));

    /** 全部后缀格式的文件标识符 **/
    public static final String ALL_SUFFIX_FLAG = "*";

    /** 不校验文件大小标识符 **/
    public static final Long ALL_MAX_SIZE = -1L;

    /** 允许上传的最大文件大小的默认值 **/
    public static final Long DEFAULT_MAX_SIZE = 100 * 1024 * 1024L;

    private static final Map<String, OssFileConfig> ALL_BIZ_TYPE_MAP = new HashMap<>();
    static{
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.H5, new OssFileConfig(OssSavePlaceEnum.PUBLIC, new HashSet<>(List.of(ALL_SUFFIX_FLAG)), DEFAULT_MAX_SIZE));
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.WECHAT, new OssFileConfig(OssSavePlaceEnum.PUBLIC, new HashSet<>(List.of(ALL_SUFFIX_FLAG)), DEFAULT_MAX_SIZE));
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.ACTIVITY, new OssFileConfig(OssSavePlaceEnum.PUBLIC, IMG_SUFFIX, DEFAULT_MAX_SIZE));
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.DOWNLOAD, new OssFileConfig(OssSavePlaceEnum.PUBLIC, new HashSet<>(List.of(ALL_SUFFIX_FLAG)), DEFAULT_MAX_SIZE));
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.UPLOAD, new OssFileConfig(OssSavePlaceEnum.PUBLIC, new HashSet<>(List.of(ALL_SUFFIX_FLAG)), DEFAULT_MAX_SIZE));
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.TEMPLATE, new OssFileConfig(OssSavePlaceEnum.PUBLIC, EXCEL_SUFFIX, DEFAULT_MAX_SIZE));
        ALL_BIZ_TYPE_MAP.put(BIZ_TYPE.USER, new OssFileConfig(OssSavePlaceEnum.PUBLIC, IMG_SUFFIX, DEFAULT_MAX_SIZE));
    }

    /** 存储位置 **/
    private OssSavePlaceEnum ossSavePlaceEnum;

    /** 允许的文件后缀, 默认全部类型 **/
    private Set<String> allowFileSuffix = new HashSet<>(List.of(ALL_SUFFIX_FLAG));

    /** 允许的文件大小, 单位： Byte **/
    private Long maxSize = DEFAULT_MAX_SIZE;


    /** 是否在允许的文件类型后缀内 **/
    public boolean isAllowFileSuffix(String fixSuffix){

        if(this.allowFileSuffix.contains(ALL_SUFFIX_FLAG)){ //允许全部
            return true;
        }

        return this.allowFileSuffix.contains(StringUtils.defaultIfEmpty(fixSuffix, "").toLowerCase());
    }

    /** 是否在允许的大小范围内 **/
    public boolean isMaxSizeLimit(Long fileSize){

        if(ALL_MAX_SIZE.equals(this.maxSize)){ //允许全部大小
            return true;
        }

        return this.maxSize >= ( fileSize == null ? 0L : fileSize);
    }


    public static OssFileConfig getOssFileConfigByBizType(String bizType){
        return ALL_BIZ_TYPE_MAP.get(bizType);
    }

}
